package api

import (
	"company_website/src/global"
	"company_website/src/model"
	"crypto/tls"
	"github.com/jordan-wright/email"
	"gorm.io/gorm"
	"math/rand"
	"net/smtp"
	"strconv"
	"time"
)

// GetAdminList 分页获取成员列表
func GetAdminList() *gorm.DB {
	tx := global.GVL_DB.Model(&model.AdminUser{}).Preload("Authorizations")
	return tx
}

// GetRand 生成随机数
func GetRand() string {
	rand.Seed(time.Now().UnixNano())
	str := ""
	for i := 0; i < 6; i++ {
		str += strconv.Itoa(rand.Intn(10))
	}
	return str
}

// SendEmail 发送验证码
func SendEmail(toUserEmail, code string) error {
	e := email.NewEmail()
	e.From = "3649668117@qq.com"
	e.To = []string{toUserEmail}
	e.Subject = "验证码已发送，请查收"
	e.HTML = []byte("您的验证码是: <b>" + code + "</b>")
	return e.SendWithTLS("smtp.qq.com:465",
		smtp.PlainAuth("", "3649668117@qq.com", "tfqljfvvidteciab", "smtp.qq.com"),
		&tls.Config{InsecureSkipVerify: true, ServerName: "smtp.qq.com"})
}

// SendCode 发送验证码
//func (cli Client) SendCode(c *gin.Context) {
//	var mail request.AdminEmail
//	err := c.ShouldBindJSON(&mail)
//	if err != nil {
//		response.FailWithMessage("绑定失败", c)
//		return
//	}
//	code := GetRand()
//	//设置键值对存入Redis
//	global.GVL_REDIS.Conn.Set(c, mail.Email, code, time.Second*300)
//	err = SendEmail(mail.Email, code)
//	if err != nil {
//		response.FailWithMessage("验证码发送失败", c)
//		return
//	}
//	response.OkWithData("验证码发送成功", c)
//}
